package com.ruoyi.cbs.project.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import com.ruoyi.cbs.approval.domain.ApprovalConfig;
import com.ruoyi.cbs.approval.mapper.ApprovalConfigMapper;
import com.ruoyi.cbs.approval.mapper.ApprovalResultMapper;
import com.ruoyi.cbs.approval.mapper.ApprovalUserMapper;
import com.ruoyi.cbs.approval.service.IApprovalResultService;
import com.ruoyi.cbs.project.domain.*;
import com.ruoyi.cbs.project.domain.vo.*;
import com.ruoyi.cbs.project.mapper.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.enums.project.ApplicationStatusEnum;
import com.ruoyi.common.enums.project.ProjectStatusEnum;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cbs.project.service.IProjectStartApplicationService;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.MaskFormatter;

/**
 * 项目开工申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-12
 */
@Service
public class ProjectStartApplicationServiceImpl implements IProjectStartApplicationService 
{
    @Autowired
    private ProjectStartApplicationMapper projectStartApplicationMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectPersonnelMapper projectPersonnelMapper;
    @Autowired
    private ProjectSafetyBriefingMapper projectSafetyBriefingMapper;
    @Autowired
    private ProjectMaterialsMapper projectMaterialsMapper;
    @Autowired
    private IApprovalResultService approvalResultService;

    /**
     * 查询项目开工申请
     * 
     * @param id 项目开工申请主键
     * @return 项目开工申请
     */
    @Override
    public ProjectStartApplication selectProjectStartApplicationById(Long id)
    {
        return projectStartApplicationMapper.selectProjectStartApplicationById(id);
    }

    /**
     * 查询项目开工申请列表
     * 
     * @param projectStartApplication 项目开工申请
     * @return 项目开工申请
     */
    @Override
    public List<ProjectStartApplication> selectProjectStartApplicationList(ProjectStartApplication projectStartApplication)
    {
        return projectStartApplicationMapper.selectProjectStartApplicationList(projectStartApplication);
    }

    @Override
    public InitApplicationRespVO getInitApplicationInfo(Long projectId) {
        //创建安全交底初始化对象
        InitApplicationRespVO respVO = new InitApplicationRespVO();
        //根据项目id获取项目信息
        Project project = projectMapper.selectProjectById(projectId);
        respVO.setApplicationNo(UUID.randomUUID().toString());
        respVO.setProjectNo(project.getProjectNo());
        respVO.setContractNo(project.getContractNo());
        respVO.setContractorName(project.getContractorName());
        respVO.setProjectName(project.getProjectName());
        return respVO;
    }

    @Override
    @Transactional
    public int startApplicationApply(StartProjectReqVO startProjectReqVO) {
        //根据id获取项目信息
        Long projectId = startProjectReqVO.getProjectId();
        Project project = projectMapper.selectProjectById(projectId);
        //前提:人员都已经完成了考核 项目状态是未开始的项目
        //1.新增开工材料
        List<ProjectMaterialsReqVO> materialsReqVOList = startProjectReqVO.getMaterialsReqVOList();
        if (CollectionUtils.isNotEmpty(materialsReqVOList) && materialsReqVOList.size() > 0 ){
            for (ProjectMaterialsReqVO material : materialsReqVOList) {
                //新增项目开工材料入参对象
                ProjectMaterials projectMaterials = new ProjectMaterials();
                BeanUtils.copyProperties(material,projectMaterials);
                projectMaterials.setProjectId(projectId);
                projectMaterials.setCreateTime(DateUtils.getNowDate());
                projectMaterialsMapper.insertProjectMaterials(projectMaterials);
            }
        }
        //2.新增开工人员
        List<ProjectPersonReqVO> personInfoList = startProjectReqVO.getPersonInfoList();
        if (CollectionUtils.isNotEmpty(personInfoList) && personInfoList.size() > 0){
            for (ProjectPersonReqVO person: personInfoList) {
                ProjectPersonnel projectPersonnel = new ProjectPersonnel();
                BeanUtils.copyProperties(person,projectPersonnel);
                projectPersonnel.setProjectId(projectId);
                projectPersonnel.setCreateTime(DateUtils.getNowDate());
                projectPersonnelMapper.insertProjectPersonnel(projectPersonnel);
            }
        }
        //3.新增安全交底信息
        if (Objects.nonNull(startProjectReqVO.getSafetyBriefingReqVO())){
            ProjectSafetyBriefing projectSafetyBriefing = new ProjectSafetyBriefing();
            BeanUtils.copyProperties(startProjectReqVO.getSafetyBriefingReqVO(), projectSafetyBriefing);
            projectSafetyBriefing.setProjectId(projectId);
            projectSafetyBriefing.setCreateTime(DateUtils.getNowDate());
        }
        //4.新增开工信息
        ProjectStartApplication projectStartApplication = new ProjectStartApplication();
        BeanUtils.copyProperties(startProjectReqVO, projectStartApplication);
        projectStartApplication.setProjectId(projectId);
        projectStartApplication.setCreateTime(DateUtils.getNowDate());
        projectStartApplication.setApplicationStatus(ApplicationStatusEnum.APPROVING.getCode());
        projectStartApplicationMapper.insertProjectStartApplication(projectStartApplication);
        //5.修改项目状态
        project.setStatus(ProjectStatusEnum.START.getCode());
        project.setUpdateTime(DateUtils.getNowDate());
        projectMapper.updateProject(project);
        //6.发起开工申请审批(将流程推到当前审批人)
        //初始化配置当前审批流程
        //approvalResultService.deleteByTargetId();
        return 1;
    }

    /**
     * 新增项目开工申请
     * 
     * @param projectStartApplication 项目开工申请
     * @return 结果
     */
    @Override
    public int insertProjectStartApplication(ProjectStartApplication projectStartApplication)
    {
        projectStartApplication.setCreateTime(DateUtils.getNowDate());
        return projectStartApplicationMapper.insertProjectStartApplication(projectStartApplication);
    }

    /**
     * 修改项目开工申请
     * 
     * @param projectStartApplication 项目开工申请
     * @return 结果
     */
    @Override
    public int updateProjectStartApplication(ProjectStartApplication projectStartApplication)
    {
        projectStartApplication.setUpdateTime(DateUtils.getNowDate());
        return projectStartApplicationMapper.updateProjectStartApplication(projectStartApplication);
    }

    /**
     * 批量删除项目开工申请
     * 
     * @param ids 需要删除的项目开工申请主键
     * @return 结果
     */
    @Override
    public int deleteProjectStartApplicationByIds(Long[] ids)
    {
        return projectStartApplicationMapper.deleteProjectStartApplicationByIds(ids);
    }

    /**
     * 删除项目开工申请信息
     * 
     * @param id 项目开工申请主键
     * @return 结果
     */
    @Override
    public int deleteProjectStartApplicationById(Long id)
    {
        return projectStartApplicationMapper.deleteProjectStartApplicationById(id);
    }
}
